export default {
  state: {
    activeTag: {
      name: "首页",
      path: "/home"
    },
    tags: [
      {
        name: "首页",
        path: "/home",
        isActive: true
      }
    ]
  },
  mutations: {
    clickTags (state,payload) {
      if(payload.path == state.activeTag.path) {
        return false
      }
      // 判断新点击标签是否存在标签组中
      let isExist = false
      // 取消旧标签的活跃状态,改变点击标签的活跃状态
      for(let tag of state.tags) {
        if(tag.path == state.activeTag.path) {
          tag.isActive = false
        }
        if(tag.path == payload.path) {
          tag.isActive = true
          isExist = true
        }
        if(payload.path.substring(16,26) == "docDetails") {
          let index = state.tags.findIndex(tag => tag.path.substring(16,26) == "docDetails")
          if(index != -1) {
            state.tags.splice(state.tags.findIndex(tag => tag.path.substring(16,26) == "docDetails"),1)
          }
          isExist = false
        }
      }
      state.activeTag = payload
      // 如果点击的标签不存在标签组中,则添加进去
      if(!isExist) {
        state.tags.push({
          name: payload.name,
          path: payload.path,
          isActive: true
        })
      }
    },
    removeTags (state,payload) {
      state.tags.splice(state.tags.findIndex(tag => tag.path == payload.path),1)
    }
  }
}